package com.conduit.comnands;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import com.conduit.WinReg.WindowsReqistry;
import com.conduit.enums.ResultCode;

public class ValidateRegistryCommand extends Command {

	public ValidateRegistryCommand() {
		commandSyntax = "validate registry\\s{2,}(.*)\\s{2,}(.*)\\s{2,}(.*)";
	}
	
	@Override
	public void executeWithArgs(String args) {
		// validate command syntax
		if (!args.matches(commandSyntax)) {
			setResult("Error in Command's syntax (\"" + args + "\")");
			setResultCode(ResultCode.Error);
			return;
		}
		// get commnad's arguments
		Matcher matcher = Pattern.compile(commandSyntax).matcher(args);
		if (matcher.find()) {
			String regKey = matcher.group(1).trim();
			String regValue = matcher.group(2).trim();
			// get expected value from command
			String expectedRegistryValue = matcher.group(3).trim();
			// get actual result
			try {
				String actualRegistryValue = WindowsReqistry.readRegistry(regKey, "\"" + regValue + "\"");
				// output validation - not CaSe SeNsAtIvE
				if (expectedRegistryValue.equalsIgnoreCase(actualRegistryValue)) {
					setResult("The Registry Value for REG_KEY: " + regKey + " REG_VALUE: " + regValue + " was "
							+ actualRegistryValue + " as Expected");
					setResultCode(ResultCode.Successes);
				} else {
					setResult("The Registry Value for REG_KEY: " + regKey + " REG_VALUE: " + regValue + " was "
							+ actualRegistryValue + " and not " + expectedRegistryValue + " as Expected");
					setResultCode(ResultCode.Error);
				}
				// if registry query did not find any value or other error in
				// query
			} catch (Exception e) {
				setResult(e.getMessage());
				setResultCode(ResultCode.Error);
			}
		}
	}
	
	@Override
	public String getCommandName() {
		return "Validate Registry Value";
	}

	@Test
	public void testFunc() throws Exception {
		//executeWithArgs("Validate Registry REG_KEY: HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main REG_VALUE: Start Page expected value: nir");
		executeWithArgs("Validate Registry REG_KEY: HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main REG_VALUE: Start Page expected value: http://www.bing.com/");
		//executeWithArgs("Validate Registry REG_KEY: HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main REG_VALUE: Home expected value: nir");
		executeWithArgs("Validate Registry REG_KEY: HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main REG_VALUE: Home expected value blab: nir");

	}

	

}
